package com.binance.healthcenter.contorller;

import com.binance.healthcenter.masterelection.MasterElection;
import com.binance.healthcenter.model.HealthCheckResult;
import com.binance.healthcenter.notification.SmsNotificationService;
import com.binance.healthcenter.notification.TelegramNotificationService;
import com.binance.healthcenter.service.CAdvisorService;
import com.binance.healthcenter.service.HealthCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HealthInfoController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private MasterElection masterElection;
    @Autowired
    private HealthCheckService healthCheckService;
    @Autowired
    private TelegramNotificationService telegramNotificationService;
    @Autowired
    private SmsNotificationService smsNotificationService;
    @Autowired
    private CAdvisorService cAdvisorService;

    @RequestMapping("/discovery")
    public String doDiscoveryService() {
        StringBuilder buf = new StringBuilder();
        List<String> serviceIds = discoveryClient.getServices();
        if (!CollectionUtils.isEmpty(serviceIds)) {
            for (String s : serviceIds) {
                System.out.println("serviceId:" + s);
                List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
                if (!CollectionUtils.isEmpty(serviceInstances)) {
                    for (ServiceInstance si : serviceInstances) {
                        buf.append("[" + si.getServiceId() + " host=" + si.getHost() + " port=" + si.getPort() + " uri="
                                + si.getUri() + "]");
                    }
                } else {
                    buf.append("no service.");
                }
            }
        }


        return buf.toString();
    }


    @GetMapping("/serviceurl")
    public Map<String, List<ServiceInstance>> serviceUrl() {
        Map<String, List<ServiceInstance>> msl = new HashMap<>();
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            List<ServiceInstance> sis = discoveryClient.getInstances(service);
            msl.put(service, sis);
        }
        return msl;
    }


    @GetMapping("/ismaster")
    public Boolean isMaster() {
        return masterElection.isMaster();
    }

    @GetMapping("/getAllHealthInfo")
    public List<HealthCheckResult> getAllHealthInfo() {
        List<HealthCheckResult> healthCheckResultList = healthCheckService.getAllHealthInfo();
        return healthCheckResultList;
    }

    @GetMapping("/getAllUnHealthInfo")
    public List<HealthCheckResult> getAllUnHealthInfo() {
        List<HealthCheckResult> healthCheckResultList = healthCheckService.getAllUnHealthInfo();
        return healthCheckResultList;
    }

    @GetMapping("/testhealth")
    public HashMap<String, Object> testhealth() {
        HashMap<String, Object> healthCheckResultList = healthCheckService.getHealth("");
        return healthCheckResultList;
    }

    @GetMapping("/tgalert")
    public String tgalert() {
        telegramNotificationService.sendTextMessage("dev_test");
        return "OK";
    }


    @GetMapping("/smsalert")
    public String smsalert() {
        List<HealthCheckResult> healthCheckResultList = healthCheckService.getHealthInfoByServiceName("streamer-persist");
        smsNotificationService.sendSmsForhealthCheckResultList(healthCheckResultList);
        return "OK";
    }

    @GetMapping("/testversion")
    public String testversion() {
        return "testversion";
    }

    @GetMapping("/testCadvisor")
    public   List<HealthCheckResult>  testCadvisor() {
        List<String> result=  cAdvisorService.getSubContainersUrl("172.16.251.90",8080,CAdvisorService.ECS_SUBCONTAINERS_URL);
        List<HealthCheckResult> healthCheckResultList= cAdvisorService.requestContainerInfo("");
        return healthCheckResultList;
    }
}
